<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/shop/include :: header"></head>
<body>
<div class="layui-fluid">
    <table id="shop_merchants_goods-table" lay-filter="shop_merchants_goods-table"></table>
</div>
<!-- 工具栏 -->
<script type="text/html" id="toolbarShopMerchantsGoods">
    <div class="layui-btn-container">
        <button shiro:hasPermission="shop:merchants_goods:add" class="layui-btn layui-btn-sm" lay-event="add"><i
                class="layui-icon layui-icon-add-circle"></i>添加
        </button>
    </div>
</script>
<!-- 操作列 -->
<script type="text/html" id="shop_merchants_goods-state">
    <div class="layui-btn-group">
        <button shiro:hasPermission="shop:merchants_goods:edit" class="layui-btn layui-btn-sm" lay-event="edit"><i
                class="layui-icon layui-icon-edit"></i></button>
        <button shiro:hasPermission="shop:merchants_goods:remove" class="layui-btn layui-btn-sm" lay-event="del"><i
                class="layui-icon layui-icon-delete"></i></button>
    </div>
</script>
<div th:include="/shop/include::footer"></div>
<script>
    var conditions = $('#form').serialize();
    conditions = decodeURIComponent(conditions, true);
    layui.use(['layer', 'jquery', 'form', 'table'], async function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        // 渲染表格
        var renderTable = function () {
            layer.load(2);
            table.render({
                elem: '#shop_merchants_goods-table',
                url: '/platform/merchantsgoods/list',
                toolbar: '#toolbarShopMerchantsGoods',
                page: true,
                id: 'shopMerchantsGoodsReload',
                where: {"conditions": conditions},
                cols: [ [   {
                    type: 'checkbox',
                    fixed: 'left'
                },
                    {
                        unresize: true,
                        sort: true,
                        width: 80,
                        align: 'center',
                        field: 'id',
                        title: '商品ID',

                    },
                    {
                        align: 'center',
                        field: 'merchantsId',
                        title: '商户ID',

                    },
                    {
                        align: 'center',
                        field: 'storeId',
                        title: '店铺ID',

                    },
                    {
                        align: 'center',
                        field: 'categoryId',
                        title: '类别ID',

                    },
                    {
                        align: 'center',
                        field: 'goodsCode',
                        title: '商品编码',

                    },
                    {
                        align: 'center',
                        field: 'goodsListTitle',
                        title: '商品列表页标题',

                    },
                    {
                        align: 'center',
                        title: '商品主图',
                        templet: function (d) {
                            return '<div class="imgview" ><img src="' + d.mainFigure + '" alt="" width="50px" height="50px"></a></div>';
                        }

                    },
                    {
                        align: 'center',
                        field: 'goodsPrice',
                        title: '单价',

                    },
                    {
                        align: 'center',
                        title: '是否包邮 0：非包邮1：包邮',
                        templet: function (d) {
                            return d.isPackageMail == 0 ? "非包邮" : "包邮"
                        }

                    },
                    {
                        align: 'center',
                        field: 'deliveryProvince',
                        title: '发货地——省',

                    },
                    {
                        align: 'center',
                        field: 'deliveryCity',
                        title: '发货地——市',

                    },
                    {
                        align: 'center',
                        field: 'deliveryArea',
                        title: '发货地——区',

                    },
                    {
                        align: 'center',
                        field: 'deliveryAddress',
                        title: '发货地——详细地址',

                    },
                    {
                        align: 'center',
                        field: 'platformReply',
                        title: '平台回复',

                    },
                    {
                        align: 'center',
                        title: '处理状态',
                        templet: function (d) {
                            return d.applyState == 10 ? "待处理" : d.applyState == 20 ? "已通过" : d.applyState == 30 ? "未通过" : d.applyState == 40 ? "已完成" : d.applyState == 50 ? "未完成" : "已撤销"
                        }
                    },
                    {
                        align: 'center',
                        title: '商品来源 0：供应商1：自营',
                        templet: function (d) {
                            return d.goodsSource == 0 ? "供应商" : "自营"
                        }

                    },
                    {
                        align: 'center',
                        title: '商品状态',
                        templet: function (d) {
                            return d.goodsState == 0 ? "正常" : d.deleteFlag == 1 ? "从列表下架" : "从仓库下架"
                        }

                    },
                    {
                        align: 'center',
                        title: '推荐状态',
                        templet: function (d) {
                            return d.recommendedState == 0 ? "非优选" : "优选"
                        }

                    },
                    {
                        align: 'center',
                        field: 'createTime',
                        title: '添加时间',

                    },
                    {
                        align: 'center',
                        field: 'updateTime',
                        title: '修改时间',

                    },
                    {
                        align: 'center',
                        title: '删除状态',
                        templet: function (d) {
                            return d.deleteFlag == 0 ? "未删除" : "已删除"
                        }

                    },
                    {
                        title: '操作',
                        toolbar: '#shop_merchants_goods-state',
                        align: 'center'
                    }   ]   ],
                done: function () {
                    layer.closeAll('loading');
                }
            });

        };
        renderTable();
        // 搜索
        var active = {
            reload: function () {
                //执行重载
                table.reload('shopMerchantsGoodsReload', {
                    page: {
                        curr: 1
                        //重新从第 1 页开始
                    },
                    where: {}
                });
            }
        };

        $('#search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(shop_merchants_goods-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add'://增加
                    openAdd();
                    break;
                case 'batchRemove'://批量删除
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg('请至少选择一个');
                        break;
                    }
                    //批量删除
                    batchRemove(data);
            }
        });

        //监听工具条
        table.on('tool(shop_merchants_goods-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {//删除
                remove(data.id);
            } else if (layEvent === 'edit') {//修改
                edit(data.id);
            }
        });

        function remove(id) {
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消']
            }, function () {
                //删除
                ajaxPostLoad('/shop/merchants_goods/remove', {
                    id: id
                });
            })
        }

        function batchRemove(data) {
            layer.confirm("确认要删除选中的'" + data.length + "'条数据吗?", {
                btn: ['确定', '取消']
            }, function () {
                var ids = new Array();
                $.each(data, function (i, row) {
                    ids[i] = row['id'];
                });
                console.log("remove->ids:" + ids);
                //批量删除
                ajaxPostLoad('/shop/merchants_goods/batchRemove', {
                    ids: ids
                });
            });
        }

        function edit(id) {
            layer.open({
                type: 2,
                title: '修改',
                maxmin: true,
                shadeClose: false, // 点击遮罩关闭层
                area: ['60%', '94%'],
                content: "/platform/page/merchants_goods/edit?id=" + id + "&url=/platform/merchantsgoods/getMerchantsGoodsById" + "&key=shopMerchantsGoods",
            });
        }

        function openAdd() {
            //询问框
            var index =  layer.confirm('请选择商品来源', {
                btn: ['供应商','自营']
            }, function(){
                layer.close(index)
                selectMerchants();
                layer.open({
                    type: 1,
                    title: '请输入商品编码',
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '94%'],
                    content: '<form class="layui-form" id="addGoods">'+
                        '<input id="goodsCode" name="goodsCode" placeholder="请输入商品编码" class="layui-input" lay-verify="required" autocomplete="off">' +
                        '<select name="merchantsId" id="merchantsId" autocomplete="off" placeholder="请选择商户" class="layui-form-item layui-input-block layui-input"> </select>' +
                        '<button lay-submit="" class="layui-btn" lay-filter="shop_merchants_goods-add">立即提交</button>' +
                        '</form>'
                });
            }, function(){
                layer.close(index)
                layer.open({
                    type: 2,
                    title: '增加自定义商品',
                    maxmin: true,
                    shadeClose: false, // 点击遮罩关闭层
                    area: ['60%', '94%'],
                    content: '/platform/page/merchants_goods/add' // iframe的url
                });
            });
        }

        function selectMerchants() {
            $.ajax({
                url: '/platform/merchantsinfo/select',
                type: "get",
                success: function (result) {
                    if (result.status == "SUCCESS") {
                        if (result.data != null) {
                            var $html = "";
                            $.each(result.data, function (index, item) {
                                $html += "<option value='" + item.id + "'>" + item.merchantsName + "</option>";
                            });
                            $("#merchantsId").append($html);
                            form.render('select');
                        }
                    } else {
                        layer.msg(result.message)
                    }
                },
                error: function (result) {
                    layer.msg("查询商户失败")
                }
            });
        }
        form.on('submit(shop_merchants_goods-add)', function () {
            $.ajax({
                url: '/platform/merchantsgoods/addMerchantsGoodsByGoodsCode?goodsCode='+$("#goodsCode").val() + '&merchantsId='+$("#merchantsId").val(),
                type: "get",
                success: function (result) {
                    if (result.status == "SUCCESS"){
                        layer.msg(result.message, {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        },function () {
                            location.reload();
                        });
                    }else {
                        layer.msg(result.message);
                    }
                },
                error: function (result) {
                    layer.msg("查询商户失败")
                }
            });
            return false;
        })
    });
</script>
</body>
<style>
    #goodsCode,#merchantsId{
        margin: 10px 10px;
        width: 98%;
    }
    #addGoods .layui-btn{
        margin-left: 46%;
    }
    #addGoods .layui-form-select{
        width: 98%;margin:10px;
    }
</style>
</html>